package com.boot.app.development.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.app.development.domain.DevelopmentDoc;
import com.boot.app.development.domain.DevelopmentDocAddition;
import com.boot.app.development.mapper.DevelopmentDocMapper;
import com.boot.app.development.service.IDevelopmentDocAdditionService;
import com.boot.app.development.service.IDevelopmentDocService;
import com.boot.app.development.vo.DocVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DevelopmentDocServiceImpl extends ServiceImpl<DevelopmentDocMapper, DevelopmentDoc> implements IDevelopmentDocService {

    private final DevelopmentDocMapper mapper;

    private final IDevelopmentDocAdditionService docAdditionService;

    public DevelopmentDocServiceImpl(
        DevelopmentDocMapper mapper,
        IDevelopmentDocAdditionService docAdditionService
    ){
        this.mapper = mapper;
        this.docAdditionService = docAdditionService;
    }


    @Override
    public DocVO getDocVOById(long id) {
        return mapper.selectDocVOById(id);
    }

    @Override
    public Page<DocVO> pageDocVO(Page<DocVO> page, DevelopmentDoc doc) {
        return mapper.selectDocVOPage(page, doc);
    }

    @Override
    @Transactional
    public void saveOrUpdateDocVO(DocVO docVO) {
        DevelopmentDoc doc = new DevelopmentDoc();
        DevelopmentDocAddition docAddition = new DevelopmentDocAddition();

        BeanUtils.copyProperties(docVO, doc);
        BeanUtils.copyProperties(docVO, docAddition);
        saveOrUpdate(doc);

        docAddition.setDocId(doc.getId());
        docAdditionService.saveOrUpdate(docAddition);
    }

    @Override
    @Transactional
    public void removeDocById(long id) {
        mapper.deleteById(id);
        docAdditionService.removeById(id);
    }

}
